package com.zhuangjie.redis.config;

import com.zhaungjie.base.utils.StringUtils;
import org.redisson.Redisson;
import org.redisson.config.SingleServerConfig;
import org.redisson.spring.cache.RedissonCacheStatisticsAutoConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.redisson.config.Config;


/**
 * Redisson分布式锁配置
 * @author manzhuangjie
 */
@Configuration
// 有些模块引入，但并不需要redisson锁，会排除redisson依赖，此时就不需要这个Bean
@ConditionalOnClass(RedissonCacheStatisticsAutoConfiguration.class)
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private String redisPort;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database:0}")
    private int database;

    @Bean
    public Redisson redisson()
    {

        System.out.println("password="+password);
        System.out.println("database="+database);
        Config config = new Config();
        SingleServerConfig ssc = config.useSingleServer().setAddress("redis://"+redisHost+":"+redisPort).setDatabase(database);
        if (! StringUtils.isBlank(password)) {
            ssc.setPassword(password);
        }
        return (Redisson) Redisson.create(config);
    }
}